<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
      <!--
        el-table 属性
          data: 表格数据, 必须是一个数组
        el-table-column
          width: 宽度, 如果不设置表示自适应
          label: 表头的文字
          prop: 字符串类型, 要在此列渲染的数据属性名
       -->
      <el-table :data="list" style="width: 100%">
        <el-table-column label="标题" prop="stem" width="400"></el-table-column>
        <el-table-column
          label="作者"
          prop="creator"
          width="140"
        ></el-table-column>
        <el-table-column
          label="点赞"
          prop="likeCount"
          width="140"
        ></el-table-column>
        <el-table-column
          label="浏览数"
          prop="views"
          width="140"
        ></el-table-column>
        <el-table-column
          label="更新时间"
          prop="createdAt"
          width="200"
        ></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="actions">
              <i
                @click="openDrawer('preview', row.id)"
                class="el-icon-view"
              ></i>
              <i @click="openDrawer('edit', row.id)" class="el-icon-edit"></i>
              <i @click="del(row.id)" class="el-icon-delete"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="q.current"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-drawer
      size="50%"
      :title="drawerTitle"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import {
  getArticles,
  addArticle,
  removeArticle,
  getArticleDetail,
  updateArticle
} from '@/api/article'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      // currentPage4: 1,
      // current: 1,
      // pageSize: 10,
      q: {
        current: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      drawer: false,
      drawerType: 'add'
    }
  },
  computed: {
    drawerTitle () {
      let title = '添加面经'
      if (this.drawerType === 'add') title = '添加面经'
      if (this.drawerType === 'edit') title = '面经编辑'
      if (this.drawerType === 'preview') title = '面经预览'
      return title
    }
  },
  created () {
    this.loadArticles()
    // console.log(res)
    // getArticles({
    //   current: this.current,
    //   pageSize: this.pageSize
    // })
  },
  methods: {
    async submit () {
      try {
        await this.$refs.form.validate()
        let res = null
        if (this.drawerType === 'add') {
          res = addArticle(this.form)
          console.log(res, 'addArticle')
        } else {
          const { id, stem, content } = this.form
          res = await updateArticle({ id, stem, content })
          console.log(res, 'updateArticle')
        }

        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.q.current = 1
        this.loadArticles()
        this.handleClose()
      } catch (error) {
        console.log(error)
      }
    },
    handleClose () {
      this.$refs.form.resetFields()
      this.form = {
        stem: '',
        content: ''
      }
      this.drawer = false
    },
    async openDrawer (type, id) {
      console.log(type)
      console.log(id)
      this.drawerType = type
      if (type !== 'add') {
        const res = await getArticleDetail(id)
        this.form = res.data
      }
      this.drawer = true
    },
    async loadArticles () {
      const res = await getArticles(this.q)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.q.current = 1
      this.q.pageSize = val
      this.loadArticles()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.q.current = val
      this.loadArticles()
    },
    async del (id) {
      // console.log(id)
      const res = await removeArticle(id)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.$message.success('删除成功')
      this.loadArticles()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
